package com.data.enter.module.sys.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.enter.module.sys.model.Aaaa;
import com.data.enter.module.sys.dao.AaaaMapper;
import com.data.enter.module.sys.model.DataImportDto;
import com.data.enter.module.sys.service.IAaaaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ligang
 * @since 2024-03-01
 */
@Service
public class AaaaServiceImpl extends ServiceImpl<AaaaMapper, Aaaa> implements IAaaaService {

    @Autowired
    private AaaaMapper aaaaMapper;

    @Override
    public List<Map<String, Object>> findPageList(Page<Map<String, Object>> page, Map<String, Object> paramMap) {
        return aaaaMapper.findPageList(page, paramMap);
    }

    /**
     * 导出数据
     * @author ligang
     */
    @SneakyThrows
    @Override
    public void exportData(HttpServletResponse response, Map<String, Object> paramMap) {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setWrapped(true);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        String fileName = "数据(" + DateUtil.formatDate(new Date())+")";
        fileName = URLEncoder.encode(fileName,"UTF-8") +".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8'zh_cn'"+ fileName);
        //List<Map<String, Object>> list = nlpQualityDataMapper.findUpdDataPage(paramMap);
        List<List<String>> allDataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            //Map<String, Object> retMap = list.get(i);
            List<String> entityDataList = new ArrayList<>();
            entityDataList.add(String.valueOf(i + 1));
            entityDataList.add("公司名称"+i);
            entityDataList.add("经营范围"+i);
            entityDataList.add("国民经济代码"+i);
            entityDataList.add("源SIC"+i);
            entityDataList.add("修改SIC"+i);
            entityDataList.add("置信度"+i);
            entityDataList.add("批次"+i);
            String updateTime = "修改时间";
            if (StrUtil.isNotBlank(updateTime)){
                updateTime = updateTime.substring(0, updateTime.length()-2);
                entityDataList.add(updateTime);
            }else {
                entityDataList.add("");
            }
            String createTime = "";
            if (StrUtil.isNotBlank(createTime)){
                createTime = createTime.substring(0, createTime.length()-2);
                entityDataList.add(createTime);
            }else {
                entityDataList.add("");
            }
            allDataList.add(entityDataList);
        }
        WriteSheet entity = EasyExcel.writerSheet(0, "Sheet")
                .head(DataImportDto.class)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .build();
        OutputStream out = response.getOutputStream();
        //处理最终的结果
        ExcelWriter excelWriter = EasyExcel.write(out).build();
        excelWriter.write(allDataList, entity);
        excelWriter.finish();
        out.close();
    }


}
